package protocol

// 此文件用来定义程序的启动和关闭
import (
	"context"
	"fmt"
	"net/http"

	"gitee.com/liuxiaowei123/vblog-3/conf"
	"gitee.com/liuxiaowei123/vblog-3/ioc"
	"github.com/gin-gonic/gin"
)

type HttpServer struct {
	hserver *http.Server
}

func NewHttpServer() *HttpServer {
	r := gin.Default()
	r1 := r.Group("/api/vblog3")
	ioc.Apihandler().RouterRegistry(r1)

	return &HttpServer{
		hserver: &http.Server{
			// 监听地址
			Addr: fmt.Sprintf("%s:%d", conf.C.Api.Apihost, conf.C.Api.Apiport),
			// 路由管理
			Handler: r,
		},
	}
}

// 扩展对象方法，实现程序启动
func (h *HttpServer) Run() error {
	fmt.Printf("启动程序，并监听：%s:%d\n", conf.C.Api.Apihost, conf.C.Api.Apiport)
	return h.hserver.ListenAndServe()
}

// 扩展对象方法，实现程序关闭；关闭时要携带上下文
func (h *HttpServer) ShutDown(c context.Context) {
	h.hserver.Shutdown(c)
}
